package com.cory.module.job.core;

import com.alibaba.fastjson.JSONObject;
import com.cory.boot.utils.SpringUtils;
import com.cory.boot.utils.SysUtils;
import com.cory.boot.utils.TaskPoolUtils;
import com.cory.module.job.core.handle.ExcelExportJobHandle;
import com.cory.module.job.domain.entity.JobAsync;
import com.cory.module.job.domain.entity.JobAsyncDetail;
import com.cory.module.job.domain.entity.JobExcelExport;
import com.cory.module.job.domain.vo.Job;
import com.cory.module.job.service.JobAsyncService;
import com.cory.module.job.service.JobExcelExportService;
import org.springframework.beans.BeanUtils;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class JobUtils {
    
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static <P, T> void addExcelExportJob(String type, String fileName, P param) {
        ExcelExportJobHandle handle = JobFactory.getExcelExportJobHandle(type);
        addExcelExportJob(type, fileName, param, handle);
    }

    public static <P, T> void addExcelExportJob(String type, String fileName, P param, ExcelExportJobHandle<P, T> handle) {
        JobExcelExport export = new JobExcelExport().setType(type).setFileName(fileName);
        addExcelExportJob(export, param, handle);
    }

    public static <P, T> void addExcelExportJob(Job job, String fileName, P param, ExcelExportJobHandle<P, T> handle) {
        JobExcelExport export = new JobExcelExport();
        BeanUtils.copyProperties(job, export);
        export.setFileName(fileName);
        addExcelExportJob(export, param, handle);
    }

    public static <P, T> void addExcelExportJob(JobExcelExport export, P param, ExcelExportJobHandle<P, T> handle) {
        export.setTenantId(SysUtils.getLoginTenantId());
        export.setSystem(SysUtils.getSystem());
        export.setTotalNum(handle.totalNum(param));
        export.setParam(JSONObject.toJSONString(param));
        export.setState(handle.getExportMode() == 1 ? 0 : -1);
        SpringUtils.getBean(JobExcelExportService.class).save(export);
        
        if (handle.getExportMode() == 1) {
            TaskPoolUtils.run(() -> handle.run(export));
        }
    }
    
    public static void addAsyncJob(Job job, Integer... dataIds) {
        JobAsync async = new JobAsync();
        BeanUtils.copyProperties(job, async);
        List<JobAsyncDetail> details = Stream.of(dataIds).map(i -> new JobAsyncDetail().setDataId(i)).collect(Collectors.toList());
        addAsyncJob(async, details);
    }

    public static void addAsyncJob(String type, Integer... dataIds) {
        List<JobAsyncDetail> details = Stream.of(dataIds).map(i -> new JobAsyncDetail().setDataId(i)).collect(Collectors.toList());
        addAsyncJob(type, details);
    }

    public static void addAsyncJob(String type, List<JobAsyncDetail> details) {
        JobAsync async = new JobAsync().setType(type);
        addAsyncJob(async, details);
    }

    public static void addAsyncJob(JobAsync async, List<JobAsyncDetail> details) {
        SpringUtils.getBean(JobAsyncService.class).save(async, details);
    }
}
